package com.test.ansy.service;

import com.test.ansy.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.Future;

@Service
public class GithubLookUpService {
    private static final Logger logger=LoggerFactory.getLogger(GithubLookUpService.class);

    private final RestTemplate template;

    public GithubLookUpService(RestTemplateBuilder templateBuilder){
        this.template=templateBuilder.build();
    }

    @Async          //表明这是一个异步任务
    public Future<User> findUser(String user) throws InterruptedException {
        logger.info("Looking up " + user);
        String url = String.format("https://api.github.com/users/%s", user);
        User results = template.getForObject(url, User.class);
        Thread.sleep(1000L);
        return new AsyncResult<>(results);
    }
}
